package cn.wjc.demo.gateway.configruation;

import cn.wjc.demo.gateway.constant.GatewayConstant;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class GatewayConfiguration {

    /**
     * 设置动态路由
     *
     * @param routeLocatorBuilder
     * @return
     */
    @Bean
    public RouteLocator customRouteLocator(RouteLocatorBuilder routeLocatorBuilder) {
        RouteLocatorBuilder.Builder routes = routeLocatorBuilder.routes();

        for (String serviceName : GatewayConstant.RPC_SERVICE_NAME_LIST) {
            // 配置路由规则ex:
            routes.route(serviceName, r -> r
                    .path(String.format("/%s/**", serviceName))
                    .uri(String.format("lb://%s", serviceName))).build();
        }
        return routes.build();
    }
}
